<?php 
class DB 
{ 
    var $base; 
    var $dane; 
    var $group; 
    var $host; 
    var $join; 
    var $order; 
    var $pola; 
    var $pass; 
    var $sql; 
    var $tabela; 
    var $user; 
    var $where; 

    function DB($host,$user,$pass,$base){ 
        $this->connect($host,$user,$pass,$base); 
    } 

    function connect($host,$user,$pass,$base){ 
        //ob_start(); start 
        $this->sql = mysql_connect($host,$user,$pass) or die("Access denied for user: '".$user."'@'".$host."' "); 

        mysql_select_db($base) or die("Base error"); 
        return true; 
    } 

    function disconnect(){ 
        mysql_close($this->sql); 
    } 

    //  $db->add("tabel",array("field"=>"value")); 
    //  np: $db->add('test',array("a"=>"b","c"=>"d")); 
    function add($tabela,$dane){ 
        $key = array_keys($dane); 
        $value = array_values($dane); 
        $il = count($key); 
        for ($i=0;$i<$il;$i++) 
        { 
            if (empty($ad)){ 
                $ad="("; 
            }else{ 
                $ad=$ad.","; 
            } 
            if (empty($val)){ 
                $val="("; 
            }else{ 
                $val=$val.","; 
            } 
            $ad=$ad.$key[$i]; 
            $val=$val."'".$value[$i]."'"; 
        } 
        $ad=$ad.")"; 
        $val=$val.")"; 
        $sql="INSERT INTO ".$tabela." ".$ad." VALUES ".$val; 
        if (mysql_query($sql)){ 
            return true; 
        }else{ 
            $this->_error(); 
            return false; 
        } 
    } 

    //  $db->update("tabel",array("field"=>"value"),"where"); 
    //  np: $db->update('test',array("a"=>"g","c"=>"h"),"a='b'"); 
    function update($tabela,$dane,$where=""){ 
        $key = array_keys($dane); 
        $value = array_values($dane); 
        $il = count($key); 
        $set=""; 
        for ($i=0;$i<$il;$i++) 
        { 
            if (!empty($set)){ 
                $set=$set.","; 
            } 
            $set=$set.$key[$i]."='".$value[$i]."'"; 
        } 
        $sql="UPDATE ".$tabela." SET ".$set." WHERE ".$where; 
        if (mysql_query($sql)){ 
            return true; 
        }else{ 
            $this->_error(); 
            return false; 
        } 
    } 

    //  $db->IsUpdate("tabel","set","where"); 
    //  np: $db->IsUpdate('test',"pageview=pageview+1","a='g'"); 
    function IsUpdate($tabela,$set,$where=""){ 
        $sql="UPDATE ".$tabela." SET ".$set." WHERE ".$where; 
        if (mysql_query($sql)){ 
            return true; 
        }else{ 
            $this->_error(); 
            return false; 
        } 
    } 

    //  $db->del("tabel","where"); 
    //  np: $db->del('test',"a='g'"); 
    function del($tabela,$where=""){ 
        $sql="DELETE FROM ".$tabela." WHERE ".$where; 
        if (mysql_query($sql)){ 
            return true; 
        }else{ 
            $this->_error(); 
            return false; 
        } 
    } 

    //  $db->num_row("tabel","field","where"); 
    //  np: $db->num_row('test','a',null); 
    function num_row($tabela,$pole,$where="") { 
        if ($where=="") { 
            $where = ""; 
        } else { 
            $where = " WHERE ".$where; 
        } 
        $sql = "SELECT COUNT(".$pole.") FROM ".$tabela.$where; 
        if($res = mysql_query($sql)){ 
            return mysql_num_rows($res); 
        }else{ 
            $this->_error(); 
            return false; 
        } 
    } 

    //  $db->num_rows("tabel","field","where"); 
    //  np: $db->num_rows('test','a',null); 
    function num_rows($tabela,$pole,$where="") { 
        if ($where=="") { 
            $where = ""; 
        } else { 
            $where = " WHERE ".$where; 
        } 
        $sql = "SELECT ".$pole." FROM ".$tabela.$where; 
        if($res = mysql_query($sql)){ 
            return mysql_num_rows($res); 
        }else{ 
            $this->_error(); 
            return false; 
        } 
    } 

    //  $res = $db->select('test',array('a','c'),null,null,null); 
    //  while ($arr = $db->fetch($res)) { 
    //          echo $arr['a']." - ".$arr['c']."<br>\n"; 
    //  } 
    function fetch($sql){ 
      //return mysql_fetch_assoc($sql); 
      if ($res = mysql_fetch_assoc($sql)){ 
            return $res; 
        }else{ 
            $this->_error(); 
            return false; 
        } 
    } 

    //  $res = $db->select('test',array('a','c'),null,null,null); 
    //  $rows = $db->rows($res); 
    function rows($sql){ 
      //return mysql_num_rows($sql); 
      if ($res = mysql_num_rows($sql)){ 
            return $res; 
        }else{ 
            $this->_error(); 
            return false; 
        } 
    } 

    //  $res = $db->select_query('SELECT pola FROM tabela WHERE warunek'); 
    function select_query($sql){ 
        if ($res = mysql_query($sql)){ 
            return $res; 
        }else{ 
            $this->_error(); 
            return false; 
        } 
    } 

    //  $db->select('tabela',array("pole","pole"),"where","join","order by","group by"); 
    //  np: $db->select('test',array("a","c"),null,null,null,null); 
    function select($tabela,$pola,$where="",$join="",$order="",$group=""){ 
        foreach ($pola as $pole) 
        { 
            if (!empty($ob)){ 
                $ob=$ob.","; 
            }else{ 
                $ob=""; 
            } 
            $ob=$ob.$pole; 
        } 
        if (isset($join)){ 
            $jo=" ".$join." "; 
        }else{ 
            $jo=" "; 
        } 
        if (isset($where)){ 
            $war=" WHERE ".$where; 
        }else{ 
            $war=""; 
        } 
        if (isset($where)){ 
            $war=" WHERE ".$where; 
        }else{ 
            $war=""; 
        } 
        if (isset($order)){ 
            $ord=" ORDER BY ".$order; 
        }else{ 
            $ord=""; 
        } 
        if (isset($group)){ 
            $gr=" GROUP BY ".$group; 
        }else{ 
            $gr=""; 
        } 
        $sql="SELECT ".$ob." from ".$tabela.$war.$jo.$ord.$gr; 
        if ($res = mysql_query($sql)){ 
            return $res; 
        }else{ 
            $this->_error(); 
            return false; 
        } 

    } 

    function _error(){ 
        $this->error[]=mysql_errno(); 
    } 

} 

/*
    $db = new DB('localhost','***','***','***'); 
    $db->del('test',"a='g'"); 
    $db->add('test',array("a"=>"b","c"=>"d")); 
    $db->update('test',array("a"=>"g","c"=>"h"),"a='b'"); 
    $db->num_rows('test','a',null); 
    $db->select('test',array("a","c"),null,null,null,null); 
    $db->select_query("SELECT * from test"); 


    $res = $db->select('test',array('a','c'),null,null,null,null); 
    while ($arr = $db->fetch($res)) { 
            echo $arr['a']." - ".$arr['c']."<br>\n"; 
    } 
    $db->disconnect(); 
*/
?> 
